package com.kenyi.co.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.kenyi.co.ui.login.Bean.AuthLoginBean;

import java.util.ArrayList;
import java.util.List;

public class SharepUtils {

    public static final String USER_ACCESSTOKEN = "user_accessToken";
    public static final String USER_LOGIN_TOKEN = "user_login_Token";
    public static final String USER_LOGIN_USERID = "user_login_userId";
    public static final String USER_LOGIN_HEADADDRESS = "user_login_headAddress";
    public static final String USER_LOGIN_USERNAME = "user_login_userName";
    public static final String USER_LOGIN_BINDPHONE = "user_login_bindPhone";
    public static final String USER_LOGIN_BINDWECHART = "user_login_bindWeChart";
    public static final String SEARCH_HISTORY = "search_history";
    public static final String USER_FAV_TAGS_lOCAL = "user_fav_tags_local";
    public static final String USER_FAV_TAGS_LIST = "user_fav_tags_list_1";
    public static final String USER_IS_LOGIN = "user_is_login";
    public static final String USER_LOGIN_ONE = "user_login_one";
    public static final String VIDEO_ONE = "video_one";
    public static final String OUT_IP = "out_ip";
    public static final String AD_ERROR = "ad_error";
    public static final String AD_PLAY_ERROR = "ad_play_error";
    public static final String IS_SC = "is_sc";


    public static void saveUserInfo(Context mContext, AuthLoginBean.DatasBean userInfo) {
        if (mContext == null) {
            return ;
        }
        SharedPreferences spf = mContext.getSharedPreferences("travel_info", Context.MODE_PRIVATE);

        Editor editor = spf.edit();

        editor.putString(USER_LOGIN_TOKEN, userInfo.getToken());
        editor.putString(USER_LOGIN_USERID, userInfo.getUserInfo().getUserId());
        editor.putString(USER_LOGIN_HEADADDRESS, userInfo.getUserInfo().getHeadAddress());
        editor.putString(USER_LOGIN_USERNAME, userInfo.getUserInfo().getUserName());
        editor.putBoolean(USER_LOGIN_BINDPHONE, userInfo.getUserInfo().isBindPhone());
        editor.putBoolean(USER_LOGIN_BINDWECHART, userInfo.getUserInfo().isBindWeChart());
        editor.commit();

    }

    public static void deleteUserInfo(Context mContext) {
        if (mContext == null) {
            return ;
        }
        SharedPreferences spf = mContext.getSharedPreferences("travel_info", Context.MODE_PRIVATE);

        Editor editor = spf.edit();

        editor.putString(USER_LOGIN_TOKEN, "");
        editor.putString(USER_LOGIN_USERID, "");
        editor.putString(USER_LOGIN_HEADADDRESS, "");
        editor.putString(USER_LOGIN_USERNAME, "");
        editor.putBoolean(USER_LOGIN_BINDPHONE, false);
        editor.putBoolean(USER_LOGIN_BINDWECHART, false);
        editor.commit();

    }

    public static void setString_info(Context mContext, String value, String tag) {
        if (mContext == null) {
            return ;
        }
        SharedPreferences spf = mContext.getSharedPreferences("travel_info", Context.MODE_PRIVATE);
        Editor meEditor = spf.edit();
        meEditor.putString(tag, value);
        meEditor.commit();
    }

    public static String getString_info(Context mContext, String tag) {
        if (mContext == null) {
            return null;
        }
        SharedPreferences spf = mContext.getSharedPreferences("travel_info", Context.MODE_PRIVATE);
        String data = spf.getString(tag, "");
        return data;
    }

    /**
     * 保存List
     *
     * @param tag
     * @param datalist
     */
    public static <T> void setShDataList(Context mContext, String tag, List<T> datalist) {
        if (mContext == null) {
            return ;
        }
        if (null == datalist || datalist.size() <= 0)
            return;
        SharedPreferences spf = mContext.getSharedPreferences("travel_info_list", Context.MODE_PRIVATE);
        Editor meEditor = spf.edit();
        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(datalist);
        meEditor.putString(tag, strJson);
        meEditor.commit();

    }

    public static void deleleShDataList(Context mContext, String tag) {
        SharedPreferences spf = mContext.getSharedPreferences("travel_info_list", Context.MODE_PRIVATE);
        Editor meEditor = spf.edit();
        meEditor.putString(tag, "");
        meEditor.commit();
    }

    /**
     * 获取List
     *
     * @param tag
     * @return
     */

    public static <T> List<T> getShDataList(Context mContext, String tag, Class<T> cls) {
        if (mContext == null) {
            return null;
        }
        List<T> datalist = new ArrayList<T>();
        SharedPreferences spf = mContext.getSharedPreferences("travel_info_list", Context.MODE_PRIVATE);
        String strJson = spf.getString(tag, "");
        if (!StringUtils.isNotEmptypro(strJson)) {
            return datalist;
        }

        JsonArray array = new JsonParser().parse(strJson).getAsJsonArray();
        for (final JsonElement elem : array) {
            datalist.add(new Gson().fromJson(elem, cls));
        }
        return datalist;

    }

}
